﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb
{
    public partial class ViewMessage : SOSecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ValidateUserCredentials(Request.Url.LocalPath);
            if (!IsPostBack)
            {
                
                int intMessageID;

                if (Page.Request.Params["a"] != null)
                {
                    intMessageID = Convert.ToInt32(Page.Request.Params["a"].ToString());

                    Message msg = new Message();
                    Facade.MessageFacade messageFacade = new Facade.MessageFacade();
                    msg = messageFacade.GetMessage(intMessageID);
                    lblMessage.Text = msg.MessageBody;

                    BusinessObjects.UserType userType = new BusinessObjects.UserType();
                    Facade.StructureFacade structureFacade = new Facade.StructureFacade();
                    userType = structureFacade.GetUserType(msg.MessageFromUserTypeID);
                    lblFrom.Text = userType.UserTypeName;

                    if (msg.UserTypeId == 1)
                    {
                        lblType.Text = "Broadcast";
                    }
                    else
                        lblType.Text = "Individual";

                    //BusinessObjects.Branch branch = new IronOne.SalesOne.BusinessObjects.Branch();
                    if (msg.UserTypeId == 2)
                    {
                        lblBranch.Text = "All Branches";
                    }
                    else if (msg.UserTypeId == 3)
                    {
                        lblBranch.Text = structureFacade.GetBranch(msg.NodeId).BranchName;
                    }
                    else if (msg.UserTypeId == 4)
                    {
                        lblUnit.Text = structureFacade.GetUnit(msg.NodeId).Name;
                        try
                        {
                            lblBranch.Text = structureFacade.GetUnit(msg.NodeId).Name;
                        }
                        catch { }
                    }

                    if (msg.SoCodeId != null)
                        lblSOCode.Text = msg.SoCodeId.ToString();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Messaging/SearchMessages.aspx");
        }
    }
}
